import Vue from 'vue'
import Router from 'vue-router'
import login from '../components/Login.vue'  //登录页面
import home from '../components/users.vue'   //左侧列表
import users from '../components/user/user.vue'   //首页
import rights from '../components/jurisdiction/rights.vue'
import roles from '../components/jurisdiction/roles.vue'  //角色权限
import ledger from '../components/good_shop/ledger.vue'   //商品分类
import params from '../components/good_shop/params.vue'  //分类参数
import goods from '../components/good_shop/goods.vue'  //商品列表
import goodsAdd from '../components/good_shop/goodAdd.vue'  //商品添加
import orders from '../components/order/order.vue' //订单管理
import reports from '../components/dateTong/reports.vue'  //数据统计

Vue.use(Router);


//未挂载的路由
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },  //登录页面
  {
    path: '/home', component: home, redirect: '/users', //登录成功
    children: [
      { path: '/users', component: users }, //用户管理页面
      { path: '/rights', component: rights },  //权限列表
      { path: '/roles', component: roles },    //角色权限
      { path: '/categories', component: ledger },  //商品分类
      { path: "/params", component: params },     //分类参数
      { path: '/goods',component: goods},       //商品列表
      { path:'/goods/add', component: goodsAdd},  //商品添加
      { path:"/orders", component:orders},      //订单管理
      { path:'/reports',component:reports}     //数据统计
    ]
  }
];

//挂载路由
const router = new Router({
  routes
});

//路由导航头
router.beforeEach((to, from, next) => {
  //to 访问地址
  //来自地址
  //放行及跳转地址
  if (to.path === '/login') return next();
  let token = window.sessionStorage.getItem('token');  //判断是否有token值，跟cookice值使用方法差不多，只是他是前端的
  if (!token) {
    return next('/login');
  }
  next();
});

//Vue.prototype.$router = router; //挂载全局路由
export default router
